﻿using AwesomeChromeFormUI.Entity;
using System.Collections.Generic;

namespace AwesomeChromeFormUI.Cache
{
    internal sealed class GlobalFormHandleCache
    {
        private Dictionary<string, FormLocationEntity> _formhandleCache;

        /// <summary>
        /// 单例模式
        /// </summary>
        private static readonly GlobalFormHandleCache _cache = null;

        public static GlobalFormHandleCache Cache
        {
            get { return _cache; }
        }

        /// <summary>
        /// 私有化构造函数
        /// </summary>
        private GlobalFormHandleCache()
        {
            this._formhandleCache = new Dictionary<string, FormLocationEntity>();
        }

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static GlobalFormHandleCache()
        {
            _cache = new GlobalFormHandleCache();
        }

        /// <summary>
        /// 新增缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, FormLocationEntity value)
        {
            this._formhandleCache.Add(key, value);
        }


        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Update(string key, FormLocationEntity value)
        {
            if (this._formhandleCache.ContainsKey(key))
            {
                this._formhandleCache[key] = value;
                return;
            }

            this.Add(key, value);
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            return this._formhandleCache.Remove(key);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public FormLocationEntity GetValueByKey(string key)
        {
            if (this._formhandleCache.ContainsKey(key))
            {
                return this._formhandleCache[key];
            }
            return null;
        }
    }
}
